package com.example.neusoft.yuejian_12.Activity;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.neusoft.yuejian_12.Fragment.AllOrderFragment;
import com.example.neusoft.yuejian_12.Fragment.ComOrderFragment;
import com.example.neusoft.yuejian_12.Fragment.ImcomOrderFragment;
import com.example.neusoft.yuejian_12.R;

public class UserSignActivity extends AppCompatActivity implements View.OnClickListener{
    private LinearLayout mAllorder;
    private LinearLayout mFinorder;
    private LinearLayout mNfinorder;
    private ImageView orderlist_return;
    private ImageButton mImgAll;
    private ImageButton mImgCom;
    private ImageButton mImgImcom;
    private Fragment mTab1;
    private Fragment mTab2;
    private Fragment mTab3;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_sign);
        LinearLayoutManager LL = new LinearLayoutManager(this);
        initView();//初始化
        initEvent();//初始化事件
        setSelect(0);//
    }

    private void initView() {
        mAllorder = (LinearLayout) findViewById(R.id.user_tab_all);
        mFinorder = (LinearLayout) findViewById(R.id.user_tab_complete);
        mNfinorder = (LinearLayout) findViewById(R.id.user_tab_imcomplete);

        mImgAll = (ImageButton) findViewById(R.id.id_tab_all_img);
        mImgCom = (ImageButton) findViewById(R.id.id_tab_complete_img);
        mImgImcom = (ImageButton) findViewById(R.id.id_tab_imcomplete_img);

        orderlist_return = (ImageView) findViewById(R.id.user_sign_back);
    }

    private void initEvent() {
        mAllorder.setOnClickListener(this);
        mFinorder.setOnClickListener(this);
        mNfinorder.setOnClickListener(this);
        orderlist_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i) {
            case 0:
                if (mTab1 == null) {
                    mTab1 = new AllOrderFragment();
                    transaction.add(R.id.id_content2, mTab1);
                } else {
                    transaction.show(mTab1);
                }
                mImgAll.setImageResource(R.drawable.ic_buy_y);
                break;
            case 1:
                if (mTab2 == null) {
                    mTab2 = new ComOrderFragment()
                    ;transaction.add(R.id.id_content2, mTab2);
                } else {
                    transaction.show(mTab2);
                }
                mImgCom.setImageResource(R.drawable.ic_buy_y);
                break;
            case 2:
                if (mTab3 == null) {
                    mTab3 = new ImcomOrderFragment();
                    transaction.add(R.id.id_content2, mTab3);
                } else {
                    transaction.show(mTab3);
                }
                mImgImcom.setImageResource(R.drawable.ic_buy_y);
                break;
            default:
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mTab1 != null) {
            transaction.hide(mTab1);
        }
        if (mTab2 != null) {
            transaction.hide(mTab2);
        }
        if (mTab3 != null) {
            transaction.hide(mTab3);
        }
    }

    public void onClick(View v) {
        resetImgs();
        switch (v.getId()) {
            case R.id.user_tab_all:
                setSelect(0);
                break;
            case R.id.user_tab_complete:
                setSelect(1);
                break;
            case R.id.user_tab_imcomplete:
                setSelect(2);
                break;
            default:
                break;
        }
    }

    private void resetImgs() {
        mImgAll.setImageResource(R.drawable.ic_buy);
        mImgCom.setImageResource(R.drawable.ic_buy);
        mImgImcom.setImageResource(R.drawable.ic_buy);
    }
}
